import requests
from bs4 import BeautifulSoup
from datetime import datetime
import time

# 隧道域名:端口号
tunnel = "us.o387.kdlfps.com:18866"

# 用户名密码方式
username = "f2142579452"
password = "qy9qsevf"
proxies = {
    "http": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": tunnel},
    "https": "http://%(user)s:%(pwd)s@%(proxy)s/" % {"user": username, "pwd": password, "proxy": tunnel},
}
list = [
  {
    "username": "vjlfg21997",
    "sessionid": "644b9dedcd43eb4555ad083c",
    "steamLoginSecure": "76561199693479019%7C%7CeyAidHlwIjogIkpXVCIsICJhbGciOiAiRWREU0EiIH0.eyAiaXNzIjogInI6MTA2NV8yNTBBNkE3Ql9CNDBBNCIsICJzdWIiOiAiNzY1NjExOTk2OTM0NzkwMTkiLCAiYXVkIjogWyAid2ViOmNvbW11bml0eSIgXSwgImV4cCI6IDE3MjYxOTc4ODAsICJuYmYiOiAxNzE3NDY5NzE0LCAiaWF0IjogMTcyNjEwOTcxNCwgImp0aSI6ICIxMDVBXzI1MEE2QTc5X0NDM0ZGIiwgIm9hdCI6IDE3MjYxMDk3MTQsICJydF9leHAiOiAxNzQ0NDI0MTY2LCAicGVyIjogMCwgImlwX3N1YmplY3QiOiAiNDUuMTQ2LjE2My4yNSIsICJpcF9jb25maXJtZXIiOiAiNDUuMTQ2LjE2My4yNSIgfQ.qubzPq2_NBM6bUzZA4b-PKCxFtIgUIzHwFpyl4wwhtsKbcWCottOU9fKszRGTRtgU4kxDSiYAqq7e4yiEI_YDg"
  },
  {
    "username": "6ce6f464",
    "sessionid": "8d62422c214417809a6447ac",
    "steamLoginSecure": "76561199494045747%7C%7CeyAidHlwIjogIkpXVCIsICJhbGciOiAiRWREU0EiIH0.eyAiaXNzIjogInI6MTA1RV8yNTBBNkE3Q19CM0M5RCIsICJzdWIiOiAiNzY1NjExOTk0OTQwNDU3NDciLCAiYXVkIjogWyAid2ViOmNvbW11bml0eSIgXSwgImV4cCI6IDE3MjYxOTY1ODIsICJuYmYiOiAxNzE3NDY5NzM1LCAiaWF0IjogMTcyNjEwOTczNSwgImp0aSI6ICIxMDU2XzI1MEE2QTc5X0NFM0Q4IiwgIm9hdCI6IDE3MjYxMDk3MzQsICJydF9leHAiOiAxNzQ0MzQxNTM2LCAicGVyIjogMCwgImlwX3N1YmplY3QiOiAiNDUuMTQ2LjE2My4yNSIsICJpcF9jb25maXJtZXIiOiAiNDUuMTQ2LjE2My4yNSIgfQ.fgzXjVJ0FffD3Nf6jobLzVe8n1Dl5ptSxL7rs4-oioJihPLI1DrOQa91WtwSA_LWuEyUj-23G6B_gH9xSTdMDA"
  },
  {
    "username": "4de0562b",
    "sessionid": "ed48610a6f8e3fa238a1ca2a",
    "steamLoginSecure": "76561199493588061%7C%7CeyAidHlwIjogIkpXVCIsICJhbGciOiAiRWREU0EiIH0.eyAiaXNzIjogInI6MTA1OV8yNTBBNkE3QV9BRTNCQSIsICJzdWIiOiAiNzY1NjExOTk0OTM1ODgwNjEiLCAiYXVkIjogWyAid2ViOmNvbW11bml0eSIgXSwgImV4cCI6IDE3MjYxOTc0NDcsICJuYmYiOiAxNzE3NDY5NzU2LCAiaWF0IjogMTcyNjEwOTc1NiwgImp0aSI6ICIxMDY1XzI1MEE2QTdCX0I2QjA2IiwgIm9hdCI6IDE3MjYxMDk3NTUsICJydF9leHAiOiAxNzQ0MDg4NjczLCAicGVyIjogMCwgImlwX3N1YmplY3QiOiAiNDUuMTQ2LjE2My4yNSIsICJpcF9jb25maXJtZXIiOiAiNDUuMTQ2LjE2My4yNSIgfQ.oc5rO1n0gaLdTUuRzOawGcLzPk7u-6bznfZyZji1OFpBCv5GK4kIImcENkWJW64gV6kyEB5vrzEAhpCSaTrLDg"
  },
  {
    "username": "71d7169e",
    "sessionid": "f88aac0c1b365b1b7ffe2f53",
    "steamLoginSecure": "76561199493844734%7C%7CeyAidHlwIjogIkpXVCIsICJhbGciOiAiRWREU0EiIH0.eyAiaXNzIjogInI6MTA2Nl8yNTBBNkE3Ql9DMEM0MiIsICJzdWIiOiAiNzY1NjExOTk0OTM4NDQ3MzQiLCAiYXVkIjogWyAid2ViOmNvbW11bml0eSIgXSwgImV4cCI6IDE3MjYxOTYzMDAsICJuYmYiOiAxNzE3NDY5Nzc2LCAiaWF0IjogMTcyNjEwOTc3NiwgImp0aSI6ICIxMDU3XzI1MEE2QTdBX0MyQTQyIiwgIm9hdCI6IDE3MjYxMDk3NzYsICJydF9leHAiOiAxNzQ0MDc4MDY2LCAicGVyIjogMCwgImlwX3N1YmplY3QiOiAiNDUuMTQ2LjE2My4yNSIsICJpcF9jb25maXJtZXIiOiAiNDUuMTQ2LjE2My4yNSIgfQ.n8NV9mZyi-8AgLGW2LNwVbQ7o3Cf3u89ojqhw37eP_FHIr2AjYkZ5rsS0YPun5dLNyw5l2uDKmBlZK_AnfHTBw"
  },
  {
    "username": "0df9fb04",
    "sessionid": "24902b050aa4921b4da78f21",
    "steamLoginSecure": "76561199493672586%7C%7CeyAidHlwIjogIkpXVCIsICJhbGciOiAiRWREU0EiIH0.eyAiaXNzIjogInI6MTA1N18yNTBBNkE3QV9DM0RGRSIsICJzdWIiOiAiNzY1NjExOTk0OTM2NzI1ODYiLCAiYXVkIjogWyAid2ViOmNvbW11bml0eSIgXSwgImV4cCI6IDE3MjYxOTc3NzcsICJuYmYiOiAxNzE3NDY5Nzk3LCAiaWF0IjogMTcyNjEwOTc5NywgImp0aSI6ICIxMDY1XzI1MEE2QTdCX0I5M0I1IiwgIm9hdCI6IDE3MjYxMDk3OTcsICJydF9leHAiOiAxNzQ0NDExMDEzLCAicGVyIjogMCwgImlwX3N1YmplY3QiOiAiNDUuMTQ2LjE2My4yNSIsICJpcF9jb25maXJtZXIiOiAiNDUuMTQ2LjE2My4yNSIgfQ.UmyP5IuVn3zeczbPngHTAm8uqmx-a1TI03kETn6HWWdaklEKSZpnxJl3epTLHjQU-tUqGNNQiURjoUcbFSjyBA"
  },
  {
    "username": "5f348f8d",
    "sessionid": "9d5f9cd233182d632f0c7af1",
    "steamLoginSecure": "76561199493829772%7C%7CeyAidHlwIjogIkpXVCIsICJhbGciOiAiRWREU0EiIH0.eyAiaXNzIjogInI6MTA1Nl8yNTBBNkE3OV9EMzU5RSIsICJzdWIiOiAiNzY1NjExOTk0OTM4Mjk3NzIiLCAiYXVkIjogWyAid2ViOmNvbW11bml0eSIgXSwgImV4cCI6IDE3MjYxOTY0MjksICJuYmYiOiAxNzE3NDY5ODE4LCAiaWF0IjogMTcyNjEwOTgxOCwgImp0aSI6ICIxMDU2XzI1MEE2QTc5X0QzNkE3IiwgIm9hdCI6IDE3MjYxMDk4MTgsICJydF9leHAiOiAxNzQ0NDcyMjgwLCAicGVyIjogMCwgImlwX3N1YmplY3QiOiAiNDUuMTQ2LjE2My4yNSIsICJpcF9jb25maXJtZXIiOiAiNDUuMTQ2LjE2My4yNSIgfQ.-fjKpVs7Vmv2Tysg3nja4v6d8PllRwG3Vtwcgu71xuZVaOTbTQmeHQIeuVUI7O1fyQtw_hUAzYl_aJzH6XCGAw"
  },
  {
    "username": "e8ca03fa",
    "sessionid": "e9b07cdbc351b5ff1edc0bf1",
    "steamLoginSecure": "76561199494165492%7C%7CeyAidHlwIjogIkpXVCIsICJhbGciOiAiRWREU0EiIH0.eyAiaXNzIjogInI6MTA1Ml8yNTBBNkE3OV9CODBENCIsICJzdWIiOiAiNzY1NjExOTk0OTQxNjU0OTIiLCAiYXVkIjogWyAid2ViOmNvbW11bml0eSIgXSwgImV4cCI6IDE3MjYxOTc3ODIsICJuYmYiOiAxNzE3NDY5ODM5LCAiaWF0IjogMTcyNjEwOTgzOSwgImp0aSI6ICIxMDU4XzI1MEE2QTdBX0JGNDlFIiwgIm9hdCI6IDE3MjYxMDk4MzksICJydF9leHAiOiAxNzQ0MDc5MzIzLCAicGVyIjogMCwgImlwX3N1YmplY3QiOiAiNDUuMTQ2LjE2My4yNSIsICJpcF9jb25maXJtZXIiOiAiNDUuMTQ2LjE2My4yNSIgfQ.pgaiCi9ata_oL2KZHf1L_F8LsFzzU4A8y8khfHhVLuLG87_QJyAKmLn1zT0JdBiknF39NfErIpClwVAjch0jBA"
  },
  {
    "username": "8c3fae39",
    "sessionid": "69d5b9884616f91b0b5f5abf",
    "steamLoginSecure": "76561199494146805%7C%7CeyAidHlwIjogIkpXVCIsICJhbGciOiAiRWREU0EiIH0.eyAiaXNzIjogInI6MTA1Nl8yNTBBNkE3OV9ENUY2NSIsICJzdWIiOiAiNzY1NjExOTk0OTQxNDY4MDUiLCAiYXVkIjogWyAid2ViOmNvbW11bml0eSIgXSwgImV4cCI6IDE3MjYxOTY5MjUsICJuYmYiOiAxNzE3NDY5ODYwLCAiaWF0IjogMTcyNjEwOTg2MCwgImp0aSI6ICIxMDUyXzI1MEE2QTc5X0I5NjRCIiwgIm9hdCI6IDE3MjYxMDk4NjAsICJydF9leHAiOiAxNzQ0NDMyNjgwLCAicGVyIjogMCwgImlwX3N1YmplY3QiOiAiNDUuMTQ2LjE2My4yNSIsICJpcF9jb25maXJtZXIiOiAiNDUuMTQ2LjE2My4yNSIgfQ.jQELsJUuAqKnmY88N0_H3d0uRA16U10ziHjYy4yxZV7Y8RPYB1RMzIdpW0lJwOZA3izZwKx4FZ-RjmGtpXbBBg"
  },
  {
    "username": "89cb8c88",
    "sessionid": "ca03287174988f5599965a72",
    "steamLoginSecure": "76561199494056132%7C%7CeyAidHlwIjogIkpXVCIsICJhbGciOiAiRWREU0EiIH0.eyAiaXNzIjogInI6MTA2Nl8yNTBBNkE3Ql9DNzMzNCIsICJzdWIiOiAiNzY1NjExOTk0OTQwNTYxMzIiLCAiYXVkIjogWyAid2ViOmNvbW11bml0eSIgXSwgImV4cCI6IDE3MjYxOTYzODYsICJuYmYiOiAxNzE3NDY5ODgxLCAiaWF0IjogMTcyNjEwOTg4MSwgImp0aSI6ICIxMDYxXzI1MEE2QTdDX0MyNUFEIiwgIm9hdCI6IDE3MjYxMDk4ODAsICJydF9leHAiOiAxNzQzOTY5MzY0LCAicGVyIjogMCwgImlwX3N1YmplY3QiOiAiNDUuMTQ2LjE2My4yNSIsICJpcF9jb25maXJtZXIiOiAiNDUuMTQ2LjE2My4yNSIgfQ.HvNEezA3gHSniUAvh-fPH6M3CFgO8eQrSXB2jIgzxC77cR6PsXEnTsAde7wSRPmAMOaGSzAa-sYY9RDnikCxAw"
  },
  {
    "username": "067e9d63",
    "sessionid": "69ebbe7b71df4fcdc8ffc3c5",
    "steamLoginSecure": "76561199493984614%7C%7CeyAidHlwIjogIkpXVCIsICJhbGciOiAiRWREU0EiIH0.eyAiaXNzIjogInI6MTA1QV8yNTBBNkE3OV9EN0FCNiIsICJzdWIiOiAiNzY1NjExOTk0OTM5ODQ2MTQiLCAiYXVkIjogWyAid2ViOmNvbW11bml0eSIgXSwgImV4cCI6IDE3MjYxOTczMTksICJuYmYiOiAxNzE3NDY5OTAyLCAiaWF0IjogMTcyNjEwOTkwMiwgImp0aSI6ICIxMDVDXzI1MEE2QTdDX0NCQ0YwIiwgIm9hdCI6IDE3MjYxMDk5MDEsICJydF9leHAiOiAxNzQ0MjMzMDk3LCAicGVyIjogMCwgImlwX3N1YmplY3QiOiAiNDUuMTQ2LjE2My4yNSIsICJpcF9jb25maXJtZXIiOiAiNDUuMTQ2LjE2My4yNSIgfQ.Fd7MAUj_ba5LcrdNzvHeGG5HcOXSTscAE-pv8WqtjZ8xxKBKgWLu8Zwxc2iJy1nhXvrcqLtEg7ii0O2hkqEMCA"
  },
  {
    "username": "f634c827",
    "sessionid": "49c7f8b437e34bc8539561bc",
    "steamLoginSecure": "76561199493849266%7C%7CeyAidHlwIjogIkpXVCIsICJhbGciOiAiRWREU0EiIH0.eyAiaXNzIjogInI6MTA2Ml8yNTBBNkE3Q19CODg0QyIsICJzdWIiOiAiNzY1NjExOTk0OTM4NDkyNjYiLCAiYXVkIjogWyAid2ViOmNvbW11bml0eSIgXSwgImV4cCI6IDE3MjYxOTc1ODksICJuYmYiOiAxNzE3NDY5OTIzLCAiaWF0IjogMTcyNjEwOTkyMywgImp0aSI6ICIxMDVFXzI1MEE2QTdDX0JGNkI3IiwgIm9hdCI6IDE3MjYxMDk5MjIsICJydF9leHAiOiAxNzQ0MTIzNTY0LCAicGVyIjogMCwgImlwX3N1YmplY3QiOiAiNDUuMTQ2LjE2My4yNSIsICJpcF9jb25maXJtZXIiOiAiNDUuMTQ2LjE2My4yNSIgfQ.RkR74Y12TLbTDz6fpWesq8FCFld1jtVgFdJVi5Q02xTmfQceIL37RNf-NAgyyQ9gT73a4k4fGe4rXsQLWbgbAg"
  }
]

index = 0

user_id = ""
api_url = ""


def get_urldata():
    global index
    try:
        current_credentials = list[index]
        sessionid = current_credentials["sessionid"]
        steamLoginSecure = current_credentials["steamLoginSecure"]
        response = requests.get(api_url)
        # print(f"get_urldata接口",response)
        # print('get_urldata接口',response.text)  # 请勿使用keep-alive复用连接(会导致隧道不能切换IP)

        soup = BeautifulSoup(response.text, "html.parser")
        element = soup.select_one(".profile_header_size")
        if element:
            miniprofile_value = element.get("data-miniprofile")
            global user_id
            user_id = miniprofile_value
            # print(miniprofile_value)
        else:
            print("未找到指定 class 的元素。")
            return
        if response.status_code == 200:
            headers = response.headers
            # print(f"headers {headers}")
            # 请求第二个接口
            get_steam_data(headers, miniprofile_value, sessionid, steamLoginSecure)
            return response.json()
        else:
            print(f"请求失败，状态码：{response.status_code}")
            # print_info(f"请求失败，状态码：{response.status_code}")
            return None
    except Exception as e:
        # print(f"发生错误：{e}")
        print(f"请求中...")
        # print_info(f"发生错误：{e}")
        return None


def get_steam_data(headers_from_first, miniprofile_value, sessionid, steamLoginSecure):
    try:
        timestamp = int(time.time())  # 获取当前时间戳
        api_url = f"https://steamcommunity.com/miniprofile/{miniprofile_value}/json"
        # api_url = f"https://steam-chat.com/miniprofile/{miniprofile_value}/json/?origin=https://steamloopback.hos?timestamp={timestamp}"
        headers = {
            "Cookie": f"cookie=sessionid={sessionid};steamLoginSecure={steamLoginSecure}",
            "User-Agent": "Apifox/1.0.0 (https://apifox.com)",
            "Accept": "*/*",
            "Host": "steamcommunity.com",
            # 'Connection': 'keep-alive'
            "Connection": "close",
            'Content-Encoding': 'gzip',
            "Cache-Control": "no-cache, no-store, must-revalidate",
            "Pragma": "no-cache",
            "Expires": "0",
        }
        print("sessionid----------------------------", sessionid)

        # print("api_url----------------------------",api_url)
        # print("headers",headers)
        response = requests.get(api_url, headers=headers,)
        # response = requests.get(api_url,proxies=proxies, headers=headers)
        print(f"get_steam_data接口------------",response)

        if response.status_code == 200:
            data = response.json()
            if "in_game" in data and "rich_presence" in data["in_game"]:
                rich_presence_value = data["in_game"]["rich_presence"]
                current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
                print(
                    "当前时间:" f"{current_time}: 地图及比分: {rich_presence_value}\n"
                )
            else:
                print("找不到 'rich_presence' 的值。")
            return response.json()
        else:
            print(f"请求失败，状态码：{response.status_code}")
            # print_info(f"请求失败，状态码：{response.status_code}")
            return None
    except Exception as e:
        # print(f"发生错误：{e}")
        print(f"请求中...")
        return None


print("请输入目标网址，按回车开始")
input_url = input()
api_url = input_url
get_urldata()

while True:
    time.sleep(2)
    if user_id:
        index = (index + 1) % len(list)
        print("当前index", index)
        get_steam_data(
            None, user_id, list[index]["sessionid"], list[index]["steamLoginSecure"]
        )
    else:
        print("用户 ID 为空，无法继续请求。")
